package com.neusoft.bizcore.activiti.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.activiti.constants.BizcoreActivitiConstants;
import com.neusoft.bizcore.activiti.dto.WorkorderDTO;
import com.neusoft.bizcore.activiti.model.WorkOrder;
import com.neusoft.bizcore.activiti.repository.WorkOrderRepository;
import com.neusoft.bizcore.web.dto.AbstractConverter;

@Component
public class WorkOrderConverter extends AbstractConverter<WorkOrder, WorkorderDTO> {

    @Autowired
    private WorkOrderRepository workOrderFormRepository;

    @Override
    public WorkOrder toModel(final WorkorderDTO dto) {

        WorkOrder model;

        if (dto.isNew()) {
            model = new WorkOrder();
        } else {
            model = this.workOrderFormRepository.getOne(dto.getId());
        }

        model.setWorkOrderNum(dto.getWorkOrderNum());
        model.setApplyUserId(dto.getApplyUserId());
        model.setApplyUserName(dto.getApplyUserName());
        model.setInstanceId(dto.getInstanceId());
        model.setStatus(dto.getStatus());
        model.setTitle(dto.getTitle());
        model.setType(dto.getType());

        return model;
    }

    @Override
    public WorkorderDTO toDTO(final WorkOrder model, final boolean forListView) {

        final WorkorderDTO dto = new WorkorderDTO();

        dto.setId(model.getId());
        dto.setApplyUserId(model.getApplyUserId());
        dto.setApplyUserName(model.getApplyUserName());
        dto.setInstanceId(model.getInstanceId());
        dto.setStatus(model.getStatus());
        dto.setTitle(model.getTitle());
        dto.setType(model.getType());
        dto.setWorkOrderNum(model.getWorkOrderNum());
        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());

        switch (model.getStatus()) {
        case temporary:
            dto.setStatusName(BizcoreActivitiConstants.WORK_ORDER_STATUS_TEMPORARY_NAME);
            break;
        case running:
            dto.setStatusName(BizcoreActivitiConstants.WORK_ORDER_STATUS_RUNNING_NAME);
            break;
        case revoked:
            dto.setStatusName(BizcoreActivitiConstants.WORK_ORDER_STATUS_REVOKED_NAME);
            break;
        case finish:
            dto.setStatusName(BizcoreActivitiConstants.WORK_ORDER_STATUS_FINISH_NAME);
            break;
        default:
            break;
        }

        return dto;
    }

}
